package structs

import (
	"encoding/json"
	"fmt"
	"regexp"
	"strings"

	tgApi "github.com/go-telegram-bot-api/telegram-bot-api"
)

type Vacancy struct {
	FormId      string `json:"formId"`
	ResponseId  string `json:"responseId"`
	ResponseUrl string `json:"responseUrl"`
	Position    string `json:"position"`
	Company     string `json:"company"`
	City        string `json:"city"`
	Remote      string `json:"remote"`
	Salary      string `json:"salary"`
	Description string `json:"description"`
	Details     string `json:"details"`
	Telegram    string `json:"telegram"`
	EditUrl     string `json:"editUrl"`
}

func NewVacancyFromJson(jsonString []byte) (result *Vacancy) {
	json.Unmarshal(jsonString, &result)
	return
}

func (vacancy *Vacancy) Message() (result string, format string) {
	format = "HTML"
	if len(vacancy.Description) > 0 {
		result = fmt.Sprintf(messageTemplateWithDescription(),
			boldText(vacancy.Position),
			boldText(vacancy.Company),
			boldText(vacancy.City),
			boldText(vacancy.Remote),
			boldText(vacancy.Salary),
			replaceLineBreaks(vacancy.Description))
	} else {
		result = fmt.Sprintf(messageTemplateWithoutDescription(),
			boldText(vacancy.Position),
			boldText(vacancy.Company),
			boldText(vacancy.City),
			boldText(vacancy.Remote),
			boldText(vacancy.Salary))
	}
	return
}

func (vacancy *Vacancy) FullKeyboard() (result tgApi.InlineKeyboardMarkup) {
	vacancy.vacancyKeyboard(&result)
	vacancy.controlKeyboard(&result)
	return
}

func (vacancy *Vacancy) ChannelKeyboard() (result tgApi.InlineKeyboardMarkup) {
	vacancy.vacancyKeyboard(&result)
	return
}

func (vacancy *Vacancy) ToJson() []byte {
	jsonString, err := json.Marshal(vacancy)
	if nil != err {
		panic(err)
	}
	return jsonString
}

func (vacancy *Vacancy) DbKey() string {
	return fmt.Sprintf("%s/%s", vacancy.FormId, vacancy.ResponseId)
}

func (vacancy *Vacancy) vacancyKeyboard(keyboard *tgApi.InlineKeyboardMarkup) {
	if len(vacancy.Telegram) > 0 {
		keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
			tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonURL("Связаться в Telegram", tgHandleToUrl(vacancy.Telegram))))
	}
	if len(vacancy.Details) > 0 && strings.HasPrefix(vacancy.Details, "http") {
		keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
			tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonURL("Подробности", vacancy.Details)))
	}
}

func (vacancy *Vacancy) controlKeyboard(keyboard *tgApi.InlineKeyboardMarkup) {
	keyboard.InlineKeyboard = append(keyboard.InlineKeyboard,
		tgApi.NewInlineKeyboardRow(tgApi.NewInlineKeyboardButtonData("Confim publishing", fmt.Sprintf("confirm:%s", vacancy.DbKey()))))
}

func boldText(original string) (result string) {
	// result = fmt.Sprintf("*%s*", original)
	result = fmt.Sprintf("<b>%s</b>", original)
	return
}

func replaceLineBreaks(original string) (result string) {
	re := regexp.MustCompile(`(?i)<br/>`)
	result = re.ReplaceAllString(original, "\n")
	return
}

func tgHandleToUrl(original string) (result string) {
	original = strings.TrimSpace(original)
	if strings.HasPrefix(original, "http://") || strings.HasPrefix(original, "https://") {
		result = original
	} else if strings.HasPrefix(original, "@") {
		result = fmt.Sprintf("https://t.me/%s", strings.TrimPrefix(original, "@"))
	} else {
		result = fmt.Sprintf("https://t.me/%s", original)
	}
	return
}

func messageTemplateWithDescription() string {
	return `
%s

Компания: %s
Город: %s, %s
Зп: %s

Краткое описание:
%s
	`
}

func messageTemplateWithoutDescription() string {
	return `
%s

Компания: %s
Город: %s, %s
Зп: %s
	`
}
